<?php

namespace App\Console\Commands\Image;

use App\Models\Digital;
use App\Services\Http;
use App\Services\LogTrait;
use Illuminate\Console\Command;

class Bs extends Command
{
    use LogTrait;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'image:bs {start=1} {end=1}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '百思不得姐图片';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @throws \Exception
     * @return mixed
     */
    public function handle()
    {
        $this->logName = $this->signature;
        try{
            $appId = 68865;
            $appSecret = '2d5dd0dd23b7460c8dbf172b5dffc694';

            $start = $this->argument('start');
            $end   = $this->argument('end');

            for ( $i = $start; $i <= $end; $i++ ) {
                $params = array(
                    'showapi_appid' => $appId,
                    'type' => 10,
                    'title' => '',
                    'page' => $i
                );
                $param = $this->buildParams($params, $appSecret);
                $url = 'http://route.showapi.com/255-1?' . $param;
                $result = file_get_contents($url);
                $result = json_decode($result, true);

                if( !isset($result['showapi_res_body']['pagebean']) ) {
                    continue;
                }
                $this->dataToDb($result['showapi_res_body']['pagebean']['contentlist']);
            }

        }catch (\Exception $e){
            $this->logWrite($e->getMessage());
            $this->logWrite($e->getTraceAsString());
        }
    }

    public function buildParams($params, $appSecret)
    {
        $paraStr = "";
        $signStr = "";
        ksort($params);
        foreach ($params as $key => $val) {
            if ($key != '' && $val != '') {
                $signStr .= $key.$val;
                $paraStr .= $key.'='.urlencode($val).'&';
            }
        }
        $signStr .= $appSecret;//排好序的参数加上secret,进行md5
        $sign = strtolower(md5($signStr));
        $paraStr .= 'showapi_sign='.$sign;//将md5后的值作为参数,便于服务器的效验
        return $paraStr;
    }

    /**
     * @author Haohuang
     * @email  huanghao1054@gmail.com
     * @since   2019/9/9
     * @param $data
     * @throws \Exception
     */
    public function dataToDb($data)
    {
        foreach ( $data as $row ) {
            $this->logWrite(json_encode($row, JSON_UNESCAPED_UNICODE));
            $item = [
                'title'         => $row['text'],
                'url'           => $row['cdn_img'],
                'like'          => $row['love'],
                'source'        => 3,
                'type'          => 1
            ];
            Digital::query()->updateOrCreate([
                'url' => $row['cdn_img']
            ], $item);
        }
    }
}
